package com.wm.machine.baselibrary.utils.net.subscriber;


import com.apkfuns.logutils.LogUtils;
import com.wm.machine.baselibrary.utils.net.error.BaseException;
import com.wm.machine.baselibrary.utils.net.error.ErrorEnum;


/**
 * Created by ${wentong.chen} on 18/1/23.
 * Rxjava 观察者封装
 * @param <T>   网络请求回调成功的数据
 */
public abstract class AbstBaseSubscriber<T> extends AbsObjectSubscriber<ResponseBaseModel<T>> {
    @Override
    public void onComplete() {
    }

    @Override
    public void onError(Throwable t) {
        LogUtils.e("http error",t.getMessage());
        onRequestFailed(t);
    }

    @Override
    public void onNext(ResponseBaseModel<T> responseBaseModel) {
        if (!"0".equals(responseBaseModel.ret)) {
            onRequestFailed(new BaseException(ErrorEnum.OTHER_ERROR));
        }
        else {
            onRequestSuccess(responseBaseModel);
        }
    }

    protected void onRequestSuccess(ResponseBaseModel<T> responseBaseModel) {
        onRequestSuccess(responseBaseModel.getInfo());
    }

    /**
     * 请求成功的回调
     * @param t
     */
    public abstract void onRequestSuccess(T t);

    /**
     * 请求失败的回调
     * @param t 错误信息
     */
    public abstract void onRequestFailed(Throwable t);

    public void doOnNetError(BaseException e) {
        // TODO: 18/2/7统一做网络请求的错误处理
    }
}
